
import { classicBehavior } from '../classic-behavior.js';
const BAM = wx.getBackgroundAudioManager();
Component({
    behaviors: [classicBehavior],
    /**
    * 组件的属性列表
    */
    properties: {
        playUrl: {
            type: String
        }
    },

    /**
    * 组件的初始数据
   */
    data: {
        playing: false,
        playIcon: '../../images/player@play.png',
        pauseIcon: '../../images/player@pause.png'
    },
    attached(){
        this._recoverStatus();
        this._monitorSwitch();
    },
    /**
    * 组件的方法列表
    */
    methods: {
        onPlay(){
            let playing = this.data.playing;
            playing = !playing;
            this.setData({
                playing: playing
            });
            if (playing){
                BAM.src = this.properties.playUrl;
                BAM.play();
            }else{
                BAM.pause();
            }
        },
        _recoverStatus(){
            if (BAM.paused){
                this.setData({
                    playing: false
                });
                return;
            }
            if (BAM.src == this.properties.playUrl){
                this.setData({
                    playing: true
                });
            }
        },
        _monitorSwitch(){
            BAM.onPlay(() => {
                this._recoverStatus();
            });
            BAM.onPause(() => {
                this._recoverStatus();
            });
            BAM.onStop(() => {
                this._recoverStatus();
            }); 
            BAM.onEnded(() => {
                this._recoverStatus();
            });
        }
    }
})
